<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { systemApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import Pagination from "@/components/Pagination/index.vue";
  import wordForm from './wordForm.vue';
  const wordFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const initialQuery = {
    page:1,
    limit:10,
    word_desc:'',
    promptly_type:''
  }
  const promptlyTypeList = [
    {type:1,label:'否'},
    {type:2,label:'是'},
  ]
  const listQuery = reactive({
    ...initialQuery
  })
  const handleDel = (row:any = {})=>{
    const { id=0 } = row
    ElMessageBox.confirm(`确定此黑名单语言吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await systemApi.delBlackWord({
        id,
      });
      loading.close();
      ToastSuccess('删除成功');
      getList();
    })
    .catch(() => {});
  }
  const handleSerach = () => {
    listQuery.page = 1;
    getList();
  };
  const handleForm = (row:any = {})=>{
    wordFormRef.value.openDialog({
      ...row,
      promptlyTypeList
    })
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await systemApi.listBlackWord(listQuery)
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">黑名单语言：</div>
          <el-input :validate-event="false" v-model="listQuery.word_desc" placeholder="请输入黑名单语言"/>
        </div>
        <div class="labelItem">
          <div class="label">封禁状态：</div>
          <el-select v-model="listQuery.promptly_type" placeholder="请选择封禁状态">
            <el-option
              label="全部"
              :value="''"
            />
            <el-option
              v-for="item in promptlyTypeList"
              :label="item.label"
              :value="item.type"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="handleForm()">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="120" prop="id" label="黑名单语言ID" />
      <el-table-column min-width="180" prop="word_desc" label="黑名单语言" />
      <el-table-column width="280" show-overflow-tooltip prop="admin_name" label="更新人" />
      <el-table-column width="180" prop="promptly_type" label="立即封禁" >
        <template #default="scoped">
          {{ promptlyTypeList.find(res=>res.type == scoped.row.promptly_type)?.label || scoped.row.promptly_type }}
        </template>
      </el-table-column>
      <el-table-column width="180" prop="updated_at" label="更新时间" />
      <el-table-column fixed="right" width="180"  label="操作" >
        <template #default="scoped">
          <el-button type="primary" @click="handleForm(scoped.row)">编辑</el-button>
          <el-button type="danger" @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <wordForm ref="wordFormRef" @update-list="getList" />
  </div>
</template>